<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Cms_Custom extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'cms_custom';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = ['sid','sn','mobile','weixin','username','gender','realname','birthday','age',
        'marry','education','job','height','weight','salary','national','car','house','spend',
        'smoking','drinking','province','province_name','city','city_name','area','area_name',
        'reside_province','reside_province_name','reside_city','reside_city_name','reside_area',
        'reside_area_name','avatar','sourcefrom','makerid','inputid','remark','state','status',
        'intention','bind','drop','droptype','is_del','create_time','update_time'
    ];

    protected $hidden = [
        'is_del',
    ];

    public static function _generateSn($prefix = 'C')
    {
        $maxId = self::max('id');
        $maxId++;
        return $prefix . str_pad($maxId, 5, '0', STR_PAD_LEFT);
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected $appends = ['gender_name','bind_name','sourcefrom_name','input_name','maker_name'];

    public function getGenderNameAttribute()
    {
        return $this->gender == 1 ? '男' : '女';
    }

    public function getBindNameAttribute()
    {
        return $this->bind == 1 ? '未入库' : '已入库';
    }

    public function getMakerNameAttribute()
    {
        if ($this->makerid){
            $user = Sys_User::find($this->makerid);
            return $user->username;
        }else{
            return '--';
        }

    }

    public function getInputNameAttribute()
    {
        if ($this->inputid){
            $user = Sys_User::find($this->inputid);
            return $user->username;
        }else{
            return '--';
        }
    }

    public function getSourcefromNameAttribute()
    {
        $params = Sys_Params::where('itemcode','sourcefrom')->first();
        $itemvalue = json_decode($params->itemvalue,true);
        $sourcefrom_name = $itemvalue[$this->sourcefrom];
        return $sourcefrom_name;
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $query->where('username', 'LIKE', '%' . $filter['username'] . '%');
            }

            if (isset($filter['mobile']) && !empty($filter['mobile'])) {
                $query->where('mobile', 'LIKE', '%' . $filter['mobile'] . '%');
            }

            if (isset($filter['gender']) && !empty($filter['gender'])) {
                $query->where('gender', $filter['gender']);
            }

            if (isset($filter['max_age']) && !empty($filter['max_age'])) {
                $query->where('age', '<=', $filter['max_age']);
            }

            if (isset($filter['min_age']) && !empty($filter['min_age'])) {
                $query->where('age', '>=',$filter['min_age']);
            }

            if (isset($filter['marry']) && !empty($filter['marry'])) {
                $query->where('marry', $filter['marry'] );
            }

            if (isset($filter['education']) && !empty($filter['education'])) {
                $query->where('education',  $filter['mobile'] );
            }

            if (isset($filter['state']) && !empty($filter['state'])) {
                $query->where('state', $filter['state']);
            }

            if (isset($filter['makerid']) && !empty($filter['makerid'])) {
                $query->where('makerid', $filter['makerid']);
            }

            if (isset($filter['bind']) && !empty($filter['bind'])) {
                $query->where('bind', $filter['bind']);
            }

            if (isset($filter['inputid']) && !empty($filter['inputid'])) {
                $query->where('inputid', $filter['inputid']);
            }

            if (isset($filter['sourcefrom']) && !empty($filter['sourcefrom'])) {
                $query->where('sourcefrom', $filter['sourcefrom']);
            }

            if (isset($filter['intention']) && !empty($filter['intention'])) {
                $query->where('intention', $filter['intention']);
            }

            $query->where('is_del', 1)->where('drop', 1);

        });
    }
}
